<?php

class tabelkontens extends CI_Controller {

    function __construct() {
        parent::__construct();
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
        } else {
            redirect('user_login/user', 'refresh');
        }
        $this->load->model('tabelkonten');
        $this->load->model('user');
        $this->load->model('user_role');
        $this->load->library('user_id');
        $this->load->helper('user_helper');
        if ($this->user_id->cek() == 0) {
            redirect('user_login/user', 'refresh');
        }
    }

    function index() {
        $data['id'] = $this->user_id->id();
        $data['i'] = 1;

        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data['rows'] = $this->tabelkonten->index();


        $data['content'] = 'tabelkontens/index';
        $this->load->view('template/user', $data);
    }

    function show() {
        $data['id'] = $this->user_id->id();
        
        $id = $this->uri->segment(3);
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data['rows'] = $this->tabelkonten->show($id);
        $data['content'] = 'tabelkontens/show';
        $this->load->view('template/user', $data);
    }

    function add() {
        $data['id'] = $this->user_id->id();
        
        #cek
        $access = $this->user_role->access($this->user_id->cek());
        $this->user_role->chek($access, 'news_add');
        $this->load->library('form_validation');
        $judul = $this->form_validation->set_rules('judul', 'Judul', 'required');



        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data['content'] = 'tabelkontens/new';
        $this->load->view('template/user', $data);
    }

    function edit() {
        $data['id'] = $this->user_id->id();
        
        $id = $this->uri->segment(3);
        #cek
        $access = $this->user_role->access($this->user_id->cek());
        $this->user_role->chek($access, 'news_edit');

        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data['rows'] = $this->tabelkonten->edit($id);
        $data['content'] = 'tabelkontens/edit';
        $this->load->view('template/user', $data);
    }

    function create() {
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $this->load->library('form_validation');
        $isi = $this->form_validation->set_rules('isi', 'isi', 'required');
        $judul = $this->form_validation->set_rules('judul', 'Judul', 'required');
        if ($this->form_validation->run() == FALSE) {
            $data['content'] = 'tabelkontens/new';
            $this->load->view('template/user', $data);
        } else {
            $judul = $this->input->post('judul', TRUE);
            $tgl_posting = date('Y-m-d');
            $judul = $this->input->post('judul', TRUE);
            $isi = $this->input->post('isi', TRUE);
            $this->tabelkonten->create($judul, $isi, $tgl_posting);

        }


    }

    function update() {
        $no_id = $this->input->post('no_id', TRUE);
        $data['access'] = $this->user_role->access($this->user_id->cek());

        $isi = $this->form_validation->set_rules('isi', 'isi', 'required');
        $judul = $this->form_validation->set_rules('judul', 'judul', 'required');
        $data['rows'] = $this->tabelkonten->edit($no_id);
        if ($this->form_validation->run() == FALSE) {
            $data['content'] = 'tabelkontens/edit';
            $this->load->view('template/user', $data);

        } else {
            $judul = $this->input->post('judul', TRUE);
            $isi = $this->input->post('isi', TRUE);
            $this->tabelkonten->update($no_id, $judul, $isi);
        }
    }

    function delete() {
        $access = $this->user_role->access($this->user_id->cek());
        $this->user_role->chek($access, 'news_delete');

        $no_id = $this->uri->segment(3);
        $this->tabelkonten->delete($no_id);
    }
}

?>
